'use strict';

angular.module('afasApp')
  .controller('RoleProfilesCtrl', ['$scope', 'Services', '$state', function ($scope, Services, $state) {

    $scope.items = [];
    $scope.params = {};

    // PAGINATION
    $scope.pagesize = 10;

    $scope.pagination = {
      current: 1
    };

    $scope.pageChanged = function(newPage) {
      getResultsPage(newPage);
    };

    function getResultsPage(pageNumber) {
      var data = $scope.params;
      data.pagesize = $scope.pagesize;
      data.page = (pageNumber - 1);
      $scope.pagination.current = pageNumber;
      Services.getItems('role_profiles', data)
      .then(function( results ) {
        console.log(results.headers);
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;
        $scope.items = results.data;
      });
    }
    getResultsPage(1);


    // function applyRemoteData( newData ) {
    //   $scope.countries = newData;
    // }

    // function loadRemoteData() {
    //   Services.getItems('countries')
    //   .then(function( results ) {
    //     applyRemoteData( results.data );
    //   });
    // }

    // loadRemoteData();

    $scope.addCountry = function () {
      $scope.errors = {};
      Services.add('countries', $scope.countryNew)
      .then(function(){
        $scope.countryNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };
  }])
  .controller('RoleProfileCreateCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.item = {
      'Roles': []
    };
    $scope.errors = {};
    $scope.ctrls = [];

    Services.getItems('roles', {'getall': true})
    .then(function(results){
      $scope.ctrls = _.groupBy(results.data, function(obj){
        return obj.Controller;
      });
      console.log($scope.ctrls);
    });

    $scope.toggleRole = function(role) {
      var items = _.where($scope.item.Roles, {Id: role.Id});
      var item = _.findWhere($scope.item.Roles, {Id: role.Id});

      if (items.length > 0) {
        $scope.item.Roles = _.without($scope.item.Roles, item);
      } else {
        $scope.item.Roles.push(role);
      }
    };

    // $scope.properties = function(){
    //   var props = [];
    //   angular.forEach($scope.ctrls, function(item){
    //     for(var prop in item){
    //       if(props.indexOf(prop) == -1)
    //         props.push(prop);
    //     }
    //   });
    //   return props;
    // };

    // $scope.match = function(item){
    //   var found = true;
    //   angular.forEach($scope.properties(), function(prop){
    //     if(prop == $scope.searchRoles){
    //       found = false;
    //       return;
    //     }
    //   });
    //   return found;
    // }

    $scope.add = function () {
      $scope.errors = {};
      Services.add('role_profiles', $scope.item)
      .then(function(){
        $scope.item = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };
  }])
  .controller('RoleProfileCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.item = {
      'Roles': []
    };
    $scope.currentID = $stateParams.id;
    $scope.errors = {};
    $scope.ctrls = [];

    Services.getItems('roles', {'getall': true})
    .then(function(results){
      $scope.ctrls = _.groupBy(results.data, function(obj){
        return obj.Controller;
      });
      // console.log($scope.ctrls);
    });

    Services.getItem('role_profiles', $stateParams.id, {'include': 'all'})
    .then(function(results){
      $scope.item = results.data;
      if (!results.data.Roles) {
        $scope.item.Roles = [];
      };
      // console.log($scope.item);
    });

    $scope.toggleRole = function(role) {
      var items = _.where($scope.item.Roles, {Id: role.Id});
      var item = _.findWhere($scope.item.Roles, {Id: role.Id});

      if (items.length > 0) {
        $scope.item.Roles = _.without($scope.item.Roles, item);
      } else {
        $scope.item.Roles.push(role);
      }
    };

    $scope.checkSelected = function(Id) {
      if ($scope.item) {
        // console.log(_.where($scope.item.Roles, { Id: Id }).length > 0);
        return _.where($scope.item.Roles, { Id: Id }).length > 0;
      };
    };

    $scope.update = function () {
      Services.update('role_profiles', $scope.item, $scope.currentID)
      .then(function(){
        $state.go( '^' );
      });
    };
  }]);